﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using ZLCMS.Business.UploadFileInfo;
using Newtonsoft.Json;
using NHibernate.Criterion;
using ZLCMS.Business.Util;
using System.Net;
using System.IO;

namespace ZLCMS.Web.SystemManage.Basics.ModuleOpt
{
    public partial class ModuleEdit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected CmsModuleData _moduleDb;
        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack || Request["opt"] == "renew")
            {
                Save();
            }
        }

        protected void InitializeGlobalValue()
        {
            string id;

            try
            {
                if (!IsPostBack)
                {
                    id = Request["uid"];

                    ViewState["uid"] = id;
                }
                else
                {
                    id = (string)ViewState["uid"];
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _moduleDb = session.CreateCriteria<CmsModuleData>().Add(Expression.Eq("Id", id)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<CmsModuleData>();
                }

                if (_moduleDb == null)
                {
                    throw new Exception("找不到对应信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                if (_moduleDb.ModuleType == "PIC")
                {
                    fc_img.Value = _moduleDb.Content;
                    fc_img.Config = JsonConvert.DeserializeObject<UploadConfig>(_moduleDb.Param1);

                    if (fc_img.Config.Thumbnail.Height > fc_img.Config.Thumbnail.Width)
                    {
                        //高比宽值大
                        fc_img.ImageHeight = 250;
                        fc_img.ImageWidth = ((250 * fc_img.Config.Thumbnail.Width) / fc_img.Config.Thumbnail.Height);
                    }
                    else
                    {
                        fc_img.ImageWidth = 250;
                        fc_img.ImageHeight = ((250 * fc_img.Config.Thumbnail.Height) / fc_img.Config.Thumbnail.Width);
                    }

                }
                else if (_moduleDb.ModuleType == "DOC" || _moduleDb.ModuleType == "ASPXSCRIPT")
                {
                    tb_content.Value = _moduleDb.Content;
                }
            }
        }

        protected string RenewAspxScript(string url)
        {
            String Result;

            WebResponse MyResponse;
            WebRequest MyRequest = System.Net.HttpWebRequest.Create(ServerUtil.GetRootURI() + url);

            MyResponse = MyRequest.GetResponse();

            using (StreamReader MyReader = new StreamReader(MyResponse.GetResponseStream(), System.Text.Encoding.Default))
            {
                Result = MyReader.ReadToEnd();
                MyReader.Close();
            }

            FileStream fs = new FileStream("c:flystudio.htm", FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);

            sw.WriteLine(Result);
            sw.Close();
            fs.Close();

            return Result;
        }

        protected void Save()
        {
            if (_moduleDb.ModuleType == "PIC")
            {
                _moduleDb.Content = fc_img.Value;

            }
            else if (_moduleDb.ModuleType == "DOC")
            {
                _moduleDb.Content = tb_content.Value;
            }
            else if (_moduleDb.ModuleType == "ASPXSCRIPT")
            {
                if (Request["opt"] == "renew")
                {
                    _moduleDb.Content = RenewAspxScript(_moduleDb.Param1);
                }
                else
                {
                    _moduleDb.Content = tb_content.Value;
                }
            }


            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                session.Update(_moduleDb);
                session.Flush();
            }

            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = "editmodule"
            });
        }
    }
}
